package com.jzsk.variable.center.common.utils.prop;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * 加载properties文件
 *
 * @author jiangli  2017/9/28
 * @version v1.0
 */
public class Prop {

    private Properties properties;

    public Prop(String fileName) {
        this(fileName, StandardCharsets.UTF_8.name());
    }

    public Prop(String fileName, String encoding) {
        this.properties = null;
        try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)) {
            if (inputStream == null) {
                throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
            }
            this.properties = new Properties();
            this.properties.load(new InputStreamReader(inputStream, encoding));
        } catch (IOException e) {
            throw new RuntimeException("Error loading properties file.", e);
        }
    }

    public Prop(File file) {
        this(file, StandardCharsets.UTF_8.name());
    }

    public Prop(File file, String encoding) {
        this.properties = null;
        if (file == null) {
            throw new IllegalArgumentException("File can not be null.");
        } else if (!file.isFile()) {
            throw new IllegalArgumentException("File not found : " + file.getName());
        } else {
            try (FileInputStream inputStream = new FileInputStream(file)) {
                this.properties = new Properties();
                this.properties.load(new InputStreamReader(inputStream, encoding));
            } catch (IOException var12) {
                throw new RuntimeException("Error loading properties file.", var12);
            }
        }
    }

    public String get(String key) {
        return this.properties.getProperty(key);
    }

    public String get(String key, String defaultValue) {
        return this.properties.getProperty(key, defaultValue);
    }

    public Integer getInt(String key) {
        return this.getInt(key, null);
    }

    public Integer getInt(String key, Integer defaultValue) {
        String value = this.properties.getProperty(key);
        return value != null ? Integer.parseInt(value.trim()) : defaultValue;
    }

    public Long getLong(String key) {
        return this.getLong(key, null);
    }

    public Long getLong(String key, Long defaultValue) {
        String value = this.properties.getProperty(key);
        return value != null ? Long.parseLong(value.trim()) : defaultValue;
    }

    public Boolean getBoolean(String key) {
        return this.getBoolean(key, null);
    }

    public Boolean getBoolean(String key, Boolean defaultValue) {
        String value = this.properties.getProperty(key);
        if (value != null) {
            value = value.toLowerCase().trim();
            if (Boolean.TRUE.toString().equals(value)) {
                return Boolean.TRUE;
            } else if (Boolean.FALSE.toString().equals(value)) {
                return Boolean.FALSE;
            } else {
                throw new RuntimeException("The value can not parse to Boolean : " + value);
            }
        } else {
            return defaultValue;
        }
    }

    public boolean containsKey(String key) {
        return this.properties.containsKey(key);
    }

    public Properties getProperties() {
        return this.properties;
    }
}
